package com.lhb.user.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author 86131
 * @version 1.0
 * @description 存储或获得验证码
 * @date 2025/4/3 下午6:19
 */
@Component
public class CodeUtil {
    @Autowired
     private RedisTemplate<String,String> redisTemplate;

    public boolean saveCode(String receiver, String code, String sendType)
    {
        if(Boolean.TRUE.equals(redisTemplate.hasKey("timer:" + receiver)))
        {
            return false;
        }
        redisTemplate.opsForValue().set("timer:"+ receiver,"标志",1,TimeUnit.MINUTES);
        redisTemplate.opsForValue().set(sendType+ receiver,code,5, TimeUnit.MINUTES);
        return true;
    }
    public String getCode(String receiver, String sendType)
    {
         return redisTemplate.opsForValue().get(sendType+ receiver);
    }
    public Boolean same(String receiver, String sendType, String code)
    {
        return Objects.equals(redisTemplate.opsForValue().get(sendType + receiver), code);
    }
}
